import { StoryboardData } from '../../../types/document'
import { ExportType, ExportOptions, ExportResult } from './types'
import { StoryboardExportManager } from './StoryboardExportManager'

// 全局导出管理器实例
const exportManager = new StoryboardExportManager()

/**
 * 快捷导出分镜脚本
 * @param storyboardData 分镜数据
 * @param type 导出类型
 * @param projectTitle 项目标题
 * @param options 额外选项
 */
export async function exportStoryboard(
  storyboardData: StoryboardData,
  type: ExportType,
  projectTitle?: string,
  options?: Partial<ExportOptions>
): Promise<ExportResult> {
  const defaultOptions: ExportOptions = {
    type,
    includeAll: true,
    includeThumbnails: false,
    includeTechnicalParams: true,
    includeStatistics: true,
    includeNotes: true,
    pageSize: 'A4',
    orientation: 'portrait',
    quality: 'high',
    ...options
  }
  
  return exportManager.exportStoryboard(storyboardData, defaultOptions, projectTitle)
}

/**
 * 批量导出多种格式
 * @param storyboardData 分镜数据
 * @param formats 导出格式列表
 * @param projectTitle 项目标题
 * @param options 基础选项
 */
export async function batchExportStoryboard(
  storyboardData: StoryboardData,
  formats: ExportType[],
  projectTitle?: string,
  options?: Partial<Omit<ExportOptions, 'type'>>
): Promise<ExportResult[]> {
  const baseOptions = {
    includeAll: true,
    includeThumbnails: false,
    includeTechnicalParams: true,
    includeStatistics: true,
    includeNotes: true,
    pageSize: 'A4' as const,
    orientation: 'portrait' as const,
    quality: 'high' as const,
    ...options
  }
  
  return exportManager.batchExport(storyboardData, formats, baseOptions, projectTitle)
}

/**
 * 快捷PDF导出
 */
export function exportToPDF(
  storyboardData: StoryboardData,
  projectTitle?: string,
  options?: Partial<ExportOptions>
): Promise<ExportResult> {
  return exportStoryboard(storyboardData, ExportType.PDF, projectTitle, options)
}

/**
 * 快捷Excel导出
 */
export function exportToExcel(
  storyboardData: StoryboardData,
  projectTitle?: string,
  options?: Partial<ExportOptions>
): Promise<ExportResult> {
  return exportStoryboard(storyboardData, ExportType.EXCEL, projectTitle, options)
}

/**
 * 快捷文本导出
 */
export function exportToText(
  storyboardData: StoryboardData,
  projectTitle?: string,
  options?: Partial<ExportOptions>
): Promise<ExportResult> {
  return exportStoryboard(storyboardData, ExportType.TEXT, projectTitle, options)
}

/**
 * 快捷Markdown导出
 */
export function exportToMarkdown(
  storyboardData: StoryboardData,
  projectTitle?: string,
  options?: Partial<ExportOptions>
): Promise<ExportResult> {
  return exportStoryboard(storyboardData, ExportType.MARKDOWN, projectTitle, options)
}

/**
 * 获取导出管理器实例
 */
export function getExportManager(): StoryboardExportManager {
  return exportManager
}

/**
 * 格式化文件大小
 */
export function formatFileSize(bytes: number): string {
  if (bytes === 0) return '0 Bytes'
  
  const k = 1024
  const sizes = ['Bytes', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

/**
 * 验证导出选项
 */
export function validateExportOptions(options: ExportOptions): {
  valid: boolean
  errors: string[]
} {
  return exportManager.validateOptions(options)
}

/**
 * 估算导出文件大小
 */
export function estimateExportSize(
  storyboardData: StoryboardData,
  options: ExportOptions
): { size: number; unit: string; description: string } {
  return exportManager.estimateFileSize(storyboardData, options)
}